﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Controllers;

namespace RecommenderSystem
{
    public partial class ComputationProgress : Form
    {
        private Dispatcher m_dDispatcher;
        private string m_sMethodType;
        private Dictionary<string, object> m_dParameters;
        private object m_oResult;

        public object ComputationResult
        {
            get
            {
                if (BackgroundWorker.IsBusy)
                    return null;
                return m_oResult;
            }

        }

        public ComputationProgress(Dispatcher dispatcher)
        {
            m_dDispatcher = dispatcher;
            InitializeComponent();
            BackgroundWorker.WorkerReportsProgress = true;
        }

        private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            m_oResult = null;
            m_dDispatcher.BackgroundWorker = BackgroundWorker;
            if (m_sMethodType == "RMSE")
            {
                int cUsers = (int)m_dParameters["cUsers"];
                string sTest = (string)m_dParameters["Test"];
                List<string> lAlgorithms = (List<string>)m_dParameters["Algorithms"];
                m_oResult = m_dDispatcher.RunStatisticalAnalysis(cUsers, sTest, lAlgorithms);
                cUsers++;
            }
        }

        private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Close();
        }

        private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            ComputationProgressBar.Maximum = 100;
            ComputationProgressBar.Value = e.ProgressPercentage;
        }
        public void RunWorkerAsync(string sMethodType, Dictionary<string, object> dParameters)
        {
            m_sMethodType = sMethodType;
            m_dParameters = dParameters;
            BackgroundWorker.RunWorkerAsync();
        }
    }
}
